package com.xbongbong.paas.elasticsearch.pojo.dto;

import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.pro.constant.ElasticConstant;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.elasticsearch.action.support.WriteRequest;

import java.io.Serializable;
import java.util.Map;

/**
 * 创建时间： 2018/12/25 10:50
 * 修改时间： 2018/12/25 10:50 梁鲁江
 * @author 梁鲁江
 * @since
 * @version
 */
@Setter
@Getter
@ToString
public class InsertDTO  implements Serializable {
    private static final long serialVersionUID = -7156589573057647050L;
    /**
     * esId(corpid_id)
     */
    private String esId;
    /**
     * 要插入的内容
     */
    private  Map<String, Object> source;
    /**
     * 对应的索引
     */
    private IndexTypeEnum indexTypeEnum;
    /**
     * index
     */
    private String index;
    /**
     * type
     */
    private String type;
    /**
     * 父id，可为空
     */
    private String parent;

    /**
     * 刷新方式
     *   NONE("false"),    异步更新的使用
        IMMEDIATE("true"),  尽量不使用
        WAIT_UNTIL("wait_for");   默认使用
     */
    private  WriteRequest.RefreshPolicy waitUntil;

    public InsertDTO() {
        this.waitUntil = WriteRequest.RefreshPolicy.NONE;
    }

    /**
     * 设置index 和type
     * @param indexTypeEnum  表单索引
     */
    public void setIndexTypeEnum(IndexTypeEnum indexTypeEnum) {
        this.indexTypeEnum = indexTypeEnum;
        this.index = indexTypeEnum.getIndex();
        this.type = indexTypeEnum.getType();
    }

    /**
     * 设置index 和type
     * @param indexTypeEnum  表单索引
     * @param indexVersion 索引别名
     */
    public void setIndexTypeEnum(IndexTypeEnum indexTypeEnum, String indexVersion) {
        this.indexTypeEnum = indexTypeEnum;
        this.index = indexTypeEnum.getIndex();
        this.type = indexTypeEnum.getType();
    }

    /**
     * 非团队使用
     * @param esId
     * @param source
     * @param indexTypeEnum
     */
    public InsertDTO(String esId, Map<String, Object> source, IndexTypeEnum indexTypeEnum) {
        this.esId = esId;
        this.source = source;
        this.indexTypeEnum = indexTypeEnum;
        this.source.put(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType());
        this.index = indexTypeEnum.getIndex();
        this.type = indexTypeEnum.getType();
        this.waitUntil =  WriteRequest.RefreshPolicy.NONE;
    }

    public InsertDTO(String esId, Map<String, Object> source, IndexTypeEnum indexTypeEnum, WriteRequest.RefreshPolicy waitUntil) {
        this.esId = esId;
        this.source = source;
        this.indexTypeEnum = indexTypeEnum;
        this.waitUntil = waitUntil;
    }
}
